package com.uaa.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.uaa.dao.SysUserDao;
import com.uaa.entity.SysUser;
import com.uaa.entity.UserDetailsInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;


@Service
public class UserDetailsServiceInfo implements UserDetailsService {

    // 自动加载WebSecurityConfig中的bcryptPasswordEncoder()方法的返回值BCryptPasswordEncoder对象
    @Autowired
    private PasswordEncoder bcryptPasswordEncoder;

    @Autowired
    SysUserDao sysUserDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        SysUser sysUser = sysUserDao.selectOne(queryWrapper.eq(SysUser::getUserName, username));
        if (sysUser==null){
            return null;
        }
        UserDetailsInfo userDetailsInfo = new UserDetailsInfo("1",username, sysUser.getPassword(), null);


        // 使用自定义的UserDetails对象UserDetailsInfo
      return  userDetailsInfo;

    }

}
